package cn.aion.crawlers;

import cn.aion.model.*;
//import cn.aion.util.HttpClientHelper;
import cn.aion.util.Helper;
import cn.aion.util.HttpClientHelper;
import cn.wanghaomiao.seimi.annotation.Crawler;
import cn.wanghaomiao.seimi.struct.Request;
import cn.wanghaomiao.seimi.struct.Response;
import cn.wanghaomiao.seimi.def.BaseSeimiCrawler;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * Created by 虚空之翼 on 2016/11/23.
 */
@Crawler(name = "basic")
public class Basic extends BaseSeimiCrawler {
    @Value("${seimiAgentHost}")
    private String seimiAgentHost;
    @Value("${seimiAgentPort}")
    private int seimiAgentPort;
    @Override
    public String seimiAgentHost() { return this.seimiAgentHost; }
    @Override
    public int seimiAgentPort() { return this.seimiAgentPort; }

    @Override
    public String[] startUrls() {
        //两个是测试去重的
        return new String[]{"http://www.acfun.cn/v/ac3560685"};
    }

    // 分页数据标识
    public static int dataFlag = 1;
    public static int pn = 1;
    @Autowired
    private Youku youkuCrawles;

    @Override
    public void start(Response response) {
        JXDocument doc = response.document();
//        logger.info(response.getContent());
        try {

            String playStr = org.apache.commons.lang3.StringUtils.join(doc.sel("//span[@class='view fl']/span[@class='sp2']/text()"),"");
            String id = response.getUrl().substring(response.getUrl().lastIndexOf("/ac") + 3);

            String url = "http://www.acfun.cn/content_view.aspx?contentId=" + id;
            push(Request.build(url, "main"));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void main(Response response) {
        JXDocument doc = response.document();
        try {
            JSONArray arr = JSON.parseArray(response.getContent());
            Integer playCount = (int)arr.get(0);

            System.out.println(arr);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
